home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / inst / inststream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-07  |  3.3 KB  |  138 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12. #include <stdio.h>
  13. #include "instP.h"
  14. #include "streampool.h"
  15. #include "transobj.h"
  16. #include "handleP.h"
  17.  
  18. Geom *
  19. InstImport( Pool *p )
  20. {
  21.     register Inst *inst = NULL;
  22.     FILE *file;
  23.     char *expect;
  24.  
  25.     if(p == NULL || (file = p->inf) == NULL)
  26.     return 0;
  27.  
  28.     if(fexpecttoken(file, "INST"))
  29.     return 0;
  30.  
  31.     for(;;) {
  32.     switch(fnextc(file, 0)) {
  33.     case EOF:
  34.     case CKET:
  35.         goto done;
  36.  
  37.     case 'u':
  38.         if(fexpectstr(file, "unit"))
  39.         goto syntax;
  40.         goto geom;
  41.  
  42.     case 'g':
  43.         if(fexpectstr(file, "geom"))
  44.         goto syntax;
  45.  
  46.       geom:
  47.         if(inst == NULL)
  48.         inst = (Inst *)GeomCCreate(NULL, InstMethods(), NULL);
  49.         expect = "geometry";
  50.         if(!GeomStreamIn(p, &inst->geomhandle, &inst->geom))
  51.         goto failed;
  52.         if(inst->geomhandle)
  53.         HandleRegister(&inst->geomhandle, (Ref *)inst,
  54.                 &inst->geom, HandleUpdRef);
  55.         break;
  56.  
  57.     case 't':        /* tlist ... or transform ... */
  58.         if(inst == NULL)
  59.         inst = (Inst *)GeomCCreate(NULL, InstMethods(), NULL);
  60.         fgetc(file);
  61.         switch(fgetc(file)) {
  62.         case 'l':
  63.         if(fexpectstr(file, "ist"))        /* "tlist" */
  64.             goto syntax;
  65.          transforms:
  66.         if(inst == NULL)
  67.             inst = (Inst *)GeomCCreate(NULL, InstMethods(), NULL);
  68.         expect = "TLIST object";
  69.         if(!GeomStreamIn(p, &inst->tlisthandle, &inst->tlist))
  70.             goto failed;
  71.         if(inst->tlisthandle)
  72.             HandleRegister(&inst->tlisthandle, (Ref *)inst,
  73.                 &inst->tlist, HandleUpdRef);
  74.         break;
  75.  
  76.         case 'r':
  77.         if(fexpectstr(file, "ansform"))        /* "transform" */
  78.             goto syntax;
  79.         if(fexpectstr(file, "s") == 0)    /* transforms = tlist */
  80.             goto transforms;
  81.         if(inst == NULL)
  82.             inst = (Inst *)GeomCCreate(NULL, InstMethods(), NULL);
  83.         expect = "transform matrix";
  84.         if(!TransStreamIn(p, &inst->axishandle, inst->axis))
  85.             goto failed;
  86.         if(inst->axishandle)
  87.             HandleRegister(&inst->axishandle, (Ref *)inst,
  88.                 inst->axis, TransUpdate);
  89.         break;
  90.  
  91.         default:
  92.         goto syntax;
  93.  
  94.         }
  95.         break;
  96.  
  97.     default:
  98.     syntax:
  99.       OOGLSyntax(file, "Couldn't read INST in \"%s\": syntax error",
  100.             p->poolname);
  101.       goto bogus;
  102.  
  103.     failed:
  104.       OOGLSyntax(file, "Couldn't read INST in \"%s\": expected %s",
  105.             PoolName(p), expect);
  106.  
  107.         bogus:
  108.       GeomDelete((Geom *)inst);
  109.       return NULL;
  110.     }
  111.     }
  112.  
  113.   done:
  114.     return (Geom *)inst;
  115. }
  116.  
  117. int
  118. InstExport( Inst *inst, Pool *pool )
  119. {
  120.     int ok = 1;
  121.  
  122.     if(inst == NULL || pool == NULL || pool->outf == NULL)
  123.     return NULL;
  124.  
  125.     fprintf(pool->outf, "INST\n");
  126.     if(inst->tlist != NULL || inst->tlisthandle != NULL) {
  127.     fprintf(pool->outf, "  tlist ");
  128.     ok &= GeomStreamOut(pool, inst->tlisthandle, inst->tlist);
  129.     } else {
  130.     ok &= TransStreamOut(pool, inst->axishandle, inst->axis);
  131.     }
  132.     if(inst->geom != NULL || inst->geomhandle != NULL) {
  133.     fprintf(pool->outf, "  geom ");
  134.     ok &= GeomStreamOut(pool, inst->geomhandle, inst->geom);
  135.     }
  136.     return ok;
  137. }
  138.